﻿using MediatR;
using SmartCity.ProcessEngine.Api.Read.Application.Command.Instance;
using SmartCity.ProcessEngine.Api.Read.Dtos;
using SmartCity.ProcessEngine.Domain.Entities;
using SmartCity.ProcessEngine.ErrorCode;
using SmartCity.ProcessEngine.Infrastructure.Interface;

namespace SmartCity.ProcessEngine.Api.Read.Application.CommandHandler.Instance
{
    /// <summary>
    /// 查找我的待办
    /// </summary>
    public class GetMyInstanceHandler : IRequestHandler<GetMyInstanceCommand, ApiPaging<MyInstanceDTO>>
    {
        private readonly IBaseRepository<InstanceModel> instanceRepo;
        private readonly IBaseRepository<FormModel> formRepo;
        private readonly IBaseRepository<FormNodeConfigModel> nodeCfgRepo;
        private readonly IBaseRepository<NodeModel> nodeRepo;
        private readonly IBaseRepository<InstanceRecardModel> recardRepo;

        public GetMyInstanceHandler(IBaseRepository<InstanceModel> instanceRepo, IBaseRepository<FormModel> formRepo, IBaseRepository<FormNodeConfigModel> nodeCfgRepo, IBaseRepository<NodeModel> nodeRepo, IBaseRepository<InstanceRecardModel> recardRepo)
        {
            this.instanceRepo = instanceRepo;
            this.formRepo = formRepo;
            this.nodeCfgRepo = nodeCfgRepo;
            this.nodeRepo = nodeRepo;
            this.recardRepo = recardRepo;
        }
        public async Task<ApiPaging<MyInstanceDTO>> Handle(GetMyInstanceCommand request, CancellationToken cancellationToken)
        {
            //我的实例表达式:当前操作人是我或者下一个操作人是我且没有逻辑删除的实例
            var instances = instanceRepo.GetAll().Where(x => !x.IsDeleted && (x.NextOperatorName == request.Me || x.CurrentOperatorName == request.Me));
            var nodeCfgs = nodeCfgRepo.GetAll().Where(x => x.IsDeleted == false);
            var nodes = nodeRepo.GetAll().Where(x => x.IsDeleted == false);
            var forms = formRepo.GetAll().Where(x => x.IsDeleted == false);
            var recards = recardRepo.GetAll().Where(x => x.IsDeleted == false);

            if (request.FormId != null)
            {//用户查询某一个流程表单的待办
                forms = forms.Where(x => x.Id == request.FormId);
            }

            var linq = from i in instances
                       join f in forms on i.FormId equals f.Id
                       select new MyInstanceDTO
                       {
                           Id = i.Id,
                           UpdateTime = i.UpdateTime,
                           CurrentOperatorName = i.CurrentOperatorName,
                           FormCode = f.FormCode,
                           FormName = f.FormName,
                           Status = i.FlowStatus,
                           CurrentNodeName = i.CurrentNodeName,
                           ProcessName = f.FormName + "-" + i.StarterName,
                           FormContent = i.FormContent
                       };

            ApiPaging<MyInstanceDTO> res = new ApiPaging<MyInstanceDTO>();
            res.TotalCount = linq.Count();
            res.PageDatas = linq.OrderBy(x => x.Id).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            res.Code = ApiEnum.Success;
            res.Msg = "查询我的待办列表成功!";

            return res;





        }
    }
}
